package com.tensquare.generator;

import java.util.Properties;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.tensquare.util.CodeGeneratorUtil;

import cn.hutool.core.util.StrUtil;

// 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
	public static final String DB_DRIVER_CLASSNAME;
	public static final String DB_URL;
	public static final String DB_USERNAME;
	public static final String DB_PASSWORD;
	public static final String PARENT_PACKAGE;
	public static final String TABLE_PREFIX;
	static {
		YamlPropertiesFactoryBean yamlMapFactoryBean = new YamlPropertiesFactoryBean();
		// 可以加载多个yml文件
		yamlMapFactoryBean.setResources(new ClassPathResource("application.yml"));
		Properties properties = yamlMapFactoryBean.getObject();
		// 获取yml里的参数
		DB_DRIVER_CLASSNAME = properties.getProperty("spring.datasource.driver-class-name");
		DB_URL = properties.getProperty("spring.datasource.url");
		DB_USERNAME = properties.getProperty("spring.datasource.username");
		DB_PASSWORD = properties.getProperty("spring.datasource.password");
		PARENT_PACKAGE = properties.getProperty("mybatis-plus.generator.parentPackage");
		TABLE_PREFIX = properties.getProperty("mybatis-plus.generator.tablePrefix");
		if (StrUtil.hasBlank(DB_DRIVER_CLASSNAME, DB_URL, DB_USERNAME, DB_PASSWORD)) {
			throw new MybatisPlusException("数据库连接信息存在为空");
		}
	}

	public static void main(String[] args) {
		CodeGeneratorUtil.createCode(DB_DRIVER_CLASSNAME, DB_URL, DB_USERNAME, DB_PASSWORD, PARENT_PACKAGE,
				TABLE_PREFIX);
	}

}